const axios = require("axios");
const Koa = require("koa");
const cors = require("koa2-cors");
const { nextTick } = require("process");

const router = require("koa-router")();
const app = new Koa();

app.use(
  cors({
    origin: function (ctx) {
      //设置允许来自指定域名请求
      return "*"; // 允许来自所有域名请求
    },
  })
);
const request = (api) => {
  return new Promise((resolve, reject) => {
    api();
  });
};

router.get("/proxy/zhihu", async (ctx, next) => {
  const params = ctx.request.query;
  // console.log(params);
  const { proxy } = ctx.header;
  delete ctx.header.proxy;

  let res = await new Promise((resolve) => {
    return axios(proxy, {
      params: params,
      headers: {
        authority: "www.zhihu.com",
        referer: "https://www.zhihu.com/",
        cookie:
          '_zap=62d2541c-3888-4ce0-aa56-d94598cdff9f; d_c0="AODkrVx6jBCPTnAMOpJPD86eZFh46fFWGqk=|1577085224"; _ga=GA1.2.743976205.1583233077; cap_id="ODhlMGU5MWUwZjllNGM3Mjg0ZWIxODJlZjVhMmUyNGQ=|1592924550|0cb70274e9173e8e6686ca8ed9339cc797caf8be"; l_cap_id="MDM1NTEwMDM2ZWJjNDI4ODhmYWFkMmEzZmQ4ZTJlYTk=|1592924550|1d08c132ed5a9b6b34fda5cedbb413857e71b59d"; client_id="MjNBMkY3Njk1MjkzQjRERDcyRTk5MjU5RjRDRjYwQUY=|1592924565|70e65e6c966d350ed98c4c64a87022f7009d973b"; q_c1=5cce7d44ea9249f481d0eddd65782def|1592924761000|1592924761000; r_cap_id="2|1:0|10:1592925463|8:r_cap_id|44:ZDM3MWJjYWIxZGE1NDZhYThkNzhkMzFlZTYzNDEwMTY=|2ab2834c2b13ca01f57fe23a38b618973599281a63def7df54a33a5bd44d686f"; _xsrf=jqy1EliWdXguzP5JOQhoVK2qs6g8Rm6O; capsion_ticket="2|1:0|10:1593000285|14:capsion_ticket|44:ZjI3YTk1N2NkODIwNGY0NThhZWU4Y2U5ZGE1ZTExNDA=|850e12fce641a021a15caabea9b728515973bb51638eadfe20c707f73b7ed0ff"; z_c0="2|1:0|10:1593000318|4:z_c0|92:Mi4xYU40S0RnQUFBQUFBNE9TdFhIcU1FQ1lBQUFCZ0FsVk5mb19nWHdEYnZzMFdNbkdTY2JwOWVPczdHd3dlbHpBY01R|f3ad8da4877edc190940d0ea3958e437375093f02c1d23db0d494bb3ac30b427"; _gid=GA1.2.732612268.1593873953; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1593606708,1593701178,1593873953,1593914598; SESSIONID=214AY2DMCweCGobxWGn65yGBejz8a3iKSqp2NGZVLdG; JOID=U1EUBUmj0Tyt5JJPPaSqo8yy1MMsmegP6L6oA3fjrlHpp_4lTUTqPvXvkkA-HnYvpzQFRx9ZoiFzr2FwjlG022o=; osd=VF8dAEOk3zWo7pVBNKGgpMK70ckrl-EK4rmmCnLpqV_govQiQ03vNPLhm0U0GXgmoj4CSRZcqCZ9pmR6iV-93mA=; tshl=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1593961288; KLBRSID=9d75f80756f65c61b0a50d80b4ca9b13|1593961771|1593960570; tst=r',
      },
    })
      .then(
        (res) => resolve(res),
        (err) => {
          console.log("返回错误结果", err);
          resolve(err);
        }
      )
      .catch((e) => {
        console.log("异常结果---", e);
        resolve(e);
      });
  });
  ctx.response.config = {};
  ctx.response.request = {};
  ctx.data = res.data;
  // ctx.status = res.status;
  // console.log("返回结果---", res.data);
  next();
  // ctx.body = res.data;
});

router.post("/proxy/zhihu", async (ctx, next) => {
  const params = ctx.request.query;
  next();
});

app.use(router.routes()); /*启动路由*/
app.use(router.allowedMethods());

// 处理返回的数据
app.use(async (ctx) => {
  ctx.body = ctx.data;
});

app.listen(9000, () => {
  console.log("proxy is runing port in 9000");
});
